/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jard.webshop.jsfbeans;

import jard.webshop.management.CategoryManagement;
import jard.webshop.nbp.Attribute;
import jard.webshop.nbp.Product;
import jard.webshop.management.ProductManagement;
import jard.webshop.nbp.Category;
import jard.webshop.util.Constants;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Named;

import javax.faces.bean.ManagedBean;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.io.Serializable;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Disaia
 */
@ManagedBean(name = "productBacking")
@SessionScoped
public class ProductBacking implements Serializable {

    private Product product;
    private String categoryName;
    private Boolean propagatePrice = false;
    private Boolean propagateDiscount = false;
    private Float price = 0f;
    private Float discount = 0f;
    private ProductManagement pm = null;
    private CategoryManagement cm = null;
    private List<Category> categories = new ArrayList<Category>();
    @ManagedProperty(value="#{cartModel}")
    private Cart cartModel;
    
    /** Creates a new instance of ProductBacking */
    public ProductBacking() {
        System.out.println("Constructor of ProductBacking");
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("jard_webshop_PU");
        ProductManagement.setEntityManagerFactory(emf);
        pm = ProductManagement.getInstance();
        product = new Product();    //pm.getProductsByName("Zombie T-shirt").get(0);
        cm = CategoryManagement.getInstance();
        categories = cm.getAllCategories();
    }
    

    public Product getProduct() {
        return this.product;
    }
    
    public String viewProduct(Product product)
    {
        product = pm.getProduct(product);
        this.product = product;
        this.price = product.getPrice();
        System.out.println("Viewing product: " + this.product.getName() + "");
        return "product";
    }

    public String editProduct(Product product)
    {
        this.product = product;
        this.price = product.getPrice();
        //pm = ProductManagement.getInstance();
        product = pm.getProduct(product);
        System.out.println("Editing product: " + this.product.getName() + "");
        return "editproduct";
    }
    
    public String saveProduct(){
        System.out.println("Saving product as: " + this.product.getName() + " (Category: " + product.getCategory() + ")");
        //pm = ProductManagement.getInstance();
        pm.saveProduct(this.product);
        if(propagateDiscount) {
            List<Product> subs = product.getGroupedWith();
            for(Product p : subs) {
                p.setDiscount(discount);
            }
        }
        if(propagatePrice) {
            List<Product> subs = product.getGroupedWith();
            for(Product p : subs) {
                p.setPrice(price);
            }
        }
        if(propagatePrice || propagateDiscount) {
            List<Product> subs = product.getGroupedWith();
            for(Product p : subs) {
                pm.saveProduct(p);
            }
        }
        return "editproducts";
    }
    
    public void addToCart() {
        if(cartModel != null) {
            System.out.println("Adding " + product + " to cart");
            cartModel.addProduct(product);
        } else {
            System.out.println("Cart was null");
        }
                
    }
    
    public List<Category> getCategories() {
        return categories;
    }

    public String getCategoryName() {
        return categoryName;
    }
    
    public void setCategoryName(String categoryName) {
        System.out.println("setCategoryName to " + categoryName);
        this.categoryName = categoryName;
        List<Category> matching = cm.getCategoriesByName(categoryName);
        if(matching != null && matching.size() > 0) {
            System.out.println("Setting category to " + matching.get(0));
            product.setCategory(matching.get(0));
        }
    }

    public Boolean getPropagatePrice() {
        return propagatePrice;
    }

    public void setPropagatePrice(Boolean propagatePrice) {
        this.propagatePrice = propagatePrice;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public Boolean getPropagateDiscount() {
        return propagateDiscount;
    }

    public void setPropagateDiscount(Boolean propagateDiscount) {
        this.propagateDiscount = propagateDiscount;
    }

    public Float getDiscount() {
        return discount;
    }

    public void setDiscount(Float discount) {
        this.discount = discount;
    }
    
    public String getCurrency() {
        return Constants.getCurrency();
    }

    public Cart getCartModel() {
        return cartModel;
    }

    public void setCartModel(Cart cartModel) {
        this.cartModel = cartModel;
    }
}
